import { ResourceComment } from './models/comment.model'
import { Server } from './models/server.model'
import { Bot } from './models/bot.model'

export interface MeiliIndexedComment {
  id: string
  author: string
  content?: string
  rating: number
  resource: string
  created: number
  modReply?: string
}

export interface MeiliIndexedServer {
  id: string
  name: string
  description: string
  shortDescription: string
  invite: string
  avatar?: string
  premiumActive: boolean
  ups: number
  created: number
  rating: number
  members?: number
  banner: number
  discordBanner?: string
  tags: number[]
  reviews: number
}

export interface MeiliIndexedBot extends Omit<MeiliIndexedServer, 'discordBanner'> {
  premiumBanner?: string
  guilds?: number
  shards?: number
  discriminator: string
}

export const makeMeiliComment = (comment: ResourceComment): MeiliIndexedComment => ({
  id: comment.id,
  author: comment.author.id,
  content: comment.content,
  rating: comment.rating,
  resource: comment.resource.id,
  created: comment.createdDate.getTime(),
  modReply: comment.modReply,
})

export const makeMeiliIndexedResource = (
  r: Server | Bot, ups: number, rating: number, reviews: number,
): MeiliIndexedServer | MeiliIndexedBot => ({
  id: r.id,
  name: r.name,
  description: r.description,
  shortDescription: r.shortDescription,
  avatar: r.avatar,
  premiumActive: r.premium?.active,
  created: r.createdDate.getTime(),
  rating, ups, reviews,
  invite: r.inviteLink,
  banner: r.standardBannerID,
  tags: r.tags,
})

export const makeMeiliServer = (server: Server, ups: number, rating: number, reviews: number): MeiliIndexedServer => ({
  ...makeMeiliIndexedResource(server, ups, rating, reviews),
  members: server.memberCount,
  discordBanner: server.bannerURL,
})

export const makeMeiliBot = (bot: Bot, ups: number, rating: number, reviews: number): MeiliIndexedBot => ({
  ...makeMeiliIndexedResource(bot, ups, rating, reviews),
  premiumBanner: bot.premium?.bannerURL,
  guilds: bot.guilds,
  shards: bot.shards,
  members: bot.members,
  discriminator: bot.discriminator,
})
